import http from '@/utils/http'
import qs from "qs";
import {configStore} from "@/store/config"
import {getDomainUrl} from "../utils/utils";

const path = require('path');
const fs = require('fs');

let storeConfig = configStore();

let HOST = getDomainUrl();
if(process.env.NODE_ENV === 'development') {
	HOST = 'http://' +  HOST;
}else {
	HOST = "https://" + HOST;
}

/// 缓存用户信息
let nameAssListApi = (function () {
	const filePath = path.join(__lib, 'ps_to_hag_name.json');
	return {
		save:function (data) {
			fs.writeFileSync(filePath, JSON.stringify(data, null, 2));
		},
		get:function () {
			try{
				return  JSON.parse( fs.readFileSync(filePath, 'utf8'));
			}catch (e){
				return {};
			}
		}
	}
})();

/**
 * 获取平博对HGA名字关联
 * @returns {Promise<*>}
 */
export const loadPsToHgaNameList = async function () {
	let url = HOST + "/webapi/name_association/list";
	url = "https://tdc.w115.net/webapi/name_association/list"
	let res = await http.get(url);
	if(res['state']) {
		nameAssListApi.save(res.data);
		storeConfig.updateNameGuanList(res.data);
	}
	return res;
}

/**
 * 提交下单记录
 * @param data
 * @param has_agreement
 * @returns {Promise<void>}
 */
export const createOrder = async function(data, has_agreement) {
	let url = HOST + "/webapi/order/create";
	let from = {
		...data,
		'new_user_name': storeConfig.setting.hga_user_name || 'app_test_name'
	}
	http.post(url, from);
}

/**
 * 提交历史记录
 * @param data
 * @returns {Promise<void>}
 */
export const createHgaOrder = async function(data){
	let url = HOST + "/webapi/hga_order_record/create";
	let from = {
		...data,
		'new_user_name': storeConfig.setting.hga_user_name || 'app_test_name',
	}
	http.post(url, from);
}

/**
 * 获取前3小时的 中单记录
 */
export const getLastOneKey = async function() {
	let url = HOST + '/webapi/order/getLastListOneKey';
	return await http.get(url);
}


/**
 * 获取禁止的联赛名字
 */
export const loadLeagueDisableList = async function() {
	let url = HOST + '/webapi/league/getLeagueDisable';
	url = "https://tdc.w115.net/webapi/league/getLeagueDisable"
	let res  = await http.get(url);
	if(res['state']) {
		storeConfig.updateLeagueDisable(res.data);
	}
	return res;
}

/**
 * 获取随机分配的下注类型
 */
export const getRandomBetType = async function () {
	let url = HOST + "/webapi/account/random_bet_type";
	let from = {
		'new_user_name':  storeConfig.setting.hga_user_name || 'app_test_name'
	}
	let res = await http.post(url, qs.stringify( from));
	if (res['state'] && storeConfig.setting.has_bet_server) {
		storeConfig.updateSettingBetTypeList(res.data);
	}
}

/**
 * 更新成功上限状态
 * @returns {Promise<void>}
 */
export const updateSucBetStatus = async function() {
	let url = HOST + "/webapi/client/update_suc_bet_status";
	url += '?new_user_name=' + (storeConfig.setting.hga_user_name || "");
	return await http.get(url);
}

/**
 * 更新非投状态
 * @returns {Promise<void>}
 */
export const updateErrBetStatus = async function() {
	let url = HOST + "/webapi/client/update_err_bet_status";
	url += '?new_user_name=' + (storeConfig.setting.hga_user_name || "");
	return await http.get(url);
}

/**
 * 更新客户端状态
 * @param maxcredit
 * @param hgaLoginStatus
 * @param par987LoginStatus
 * @param version
 * @param has_bet
 * @returns {Promise<void>}
 */
export const updateClientOnlineStatus = async function(maxcredit, hgaLoginStatus, par987LoginStatus, version, has_bet = 0, has_agreement_bet = 0) {

	let url = HOST + "/webapi/client/online_status";

	let bet_list = storeConfig.setting.bet_type_list.filter((item) => {
		return !!item.checked;
	})
	let bet_type_list = [];
	bet_list.forEach((item) => {
		bet_type_list.push(item.type);
	})
	let has_bet_server = storeConfig.setting.has_bet_server ? 1 : 0;

	let from = {
		"new_user_name": storeConfig.setting.hga_user_name,
		"bet_type_list": bet_type_list.join(","),
		"has_bet_server": has_bet_server,
		"balance": maxcredit||0,
		'hga_login_status': hgaLoginStatus,
		'par_login_status': par987LoginStatus,
		'version': version,
		'has_bet': has_bet,
		'has_agreement_bet': has_agreement_bet
	};
	let res = await http.post(url, qs.stringify( from));
}

/**
 * 获取现有的key数量
 * @param key
 * @returns {Promise<*>}
 */
export const getOrderKeyCount = async function(key) {
	let url = HOST + "/webapi/order/getOrderKeyCount?key=" + key;
	return await http.get(url);
}

// console.log(getRandomBetType());

// sendXHRRequest("post",webDomain + "/webapi/order/create", {...data,'new_user_name':webSocketUserName}, function (res) {
// 	// console.log(res);
// });